import docker

from runner import my_run_on_executor

client = docker.from_env()

container_common_args = {
    'detach': True,
    'stdin_open': True,
    'tty': True
}


@my_run_on_executor
def create_container(args):
    return client.containers.create(**args)


@my_run_on_executor
def get_container(containerid):
    try:
        return client.containers.get(containerid)
    except:
        return None


@my_run_on_executor
def stop_container(container):
    container.stop()


@my_run_on_executor
def stop_and_remove_container(container):
    container.stop()
    container.remove()


@my_run_on_executor
def stop_and_remove_container_by_id(containerid):
    try:
        container = client.containers.get(containerid)
        container.stop()
        container.remove()
        return True
    except:
        return False


@my_run_on_executor
def pull_image(img):
    return client.images.pull(img)


container_common_args = {
    'detach': True,
    'stdin_open': True,
    'tty': True
}
